// pages/doctorPages/my/orderPerson/orderPerson.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    checkIndex: 0,
    normalNumber: 1,
    radioIndex: 0,
    rules: {},
    morning: {
      number: 1,
      special: 1,
      special_number: 1,
      members: {
        1: "9:00"
      },
      special_rules: {
        before: 1,
        time_type: 0,
        money: "10"
      },
      before_time: 1,
      time_type: 2
    },
    selectData: {},
    currentDate: "morning",
    afternoon: {
      number: 1,
      special: 0,
      special_number: 1,
      members: {
        1: "14:00"
      },
      special_rules: {
        before: 1,
        time_type: 0,
        money: "10"
      },
      before_time: 1,
      time_type: 2
    }
  },

  inputChange: function (e) {
    this.setData({
      normalNumber: parseInt(e.detail.value)
    })
  },
  openClick: function (e) {
    let index = e.currentTarget.dataset.check;
    this.setData({
      checkIndex: index
    })
  },
  
  changeDate(e) {
    let v = e.currentTarget.dataset.value;
    this.setData({
      currentDate: v
    })
    if (v == 'morning') {
      this.setData({
        selectData: this.data.morning
      })
    } else {
      this.setData({
        selectData: this.data.afternoon
      })
    }
  },
  chengeNumber(e) {
    let v = e.detail.value;
    let data = this.data.selectData;
    data.number = parseInt(e.detail.value);
    this.setData({
      selectData: data
    });
    this.setData2MF();
  },
  chengeSpecialBeforeTime(e) {
    let v = e.currentTarget.dataset.time;
    let data = this.data.selectData;
    data.special_rules.time_type = parseInt(v);
    this.setData({
      selectData: data
    });
    this.setData2MF();
  },
  chengeSpecialBefore(e) {
    let v = e.detail.value;
    let data = this.data.selectData;
    data.special_rules.before = parseInt(v);
    this.setData({
      selectData: data
    });
    this.setData2MF();
  },
  chengeSpecialMoney(e) {
    let v = e.detail.value;
    let data = this.data.selectData;
    data.special_rules.money = parseFloat(v).toFixed(2);
    this.setData({
      selectData: data
    });
    this.setData2MF();
  },
  chengeSpecial(e) {
    let v = e.detail.value;
    let data = this.data.selectData;
    data.special = parseInt(e.currentTarget.dataset.check);
    this.setData({
      selectData: data
    });
    this.setData2MF();
  },
  chengeSpecialNumber(e) {
    let v = e.detail.value;
    if (v > this.data.selectData.number) {
      this.setData({
        selectData: this.data.selectData
      });
      return wx.showToast({
        title: '特需人数不可大于正常人数',
        icon: "none"
      });
    }
    let data = this.data.selectData;
    data.special_number = parseInt(e.detail.value);
    this.setData({
      selectData: data
    });
    this.setData2MF();
  },
  bindTimeChange(e) {
    let data = this.data.selectData;
    let key = e.currentTarget.dataset.key;
    let d = e.detail.value;
    let dv = parseInt(d.split(":").join(""));
    if (key >= 1) {
      if (!data.members[key]) {
        return wx.showToast({
          title: '请先设置上一个时间',
          icon: "none"
        })
      }
      let before = parseInt(data.members[key].split(":").join(""));
      if (dv < before) {
        return wx.showToast({
          title: '时间需大于上一个时间',
          icon: "none"
        })
      }
    }
    if (this.data.currentDate == 'morning') {
      if (dv > 1200) {
        return wx.showToast({
          title: '上午设置不可大于12:00',
          icon: "none"
        })
      }
    } else {
      if (dv <= 1200) {
        return wx.showToast({
          title: '下午设置不可小于于12:00',
          icon: "none"
        })
      }
    }
    data.members[key + 1] = d;
    this.setData({
      selectData: data
    });
    this.setData2MF();
  },
  //对设置的信息进行赋值到上午或者下午信息
  setData2MF() {
    if (this.data.currentDate == 'morning') {
      this.setData({
        morning: this.parseData(this.data.selectData)
      })
    } else {
      this.setData({
        afternoon: this.parseData(this.data.selectData)
      })
    }
  },
  changeBeforeTime(e) {
    let v = e.detail.value;
    let selectData = this.data.selectData
    selectData.before_time = v
    console.log(selectData)
    this.setData({
      selectData: selectData
    });
    this.setData2MF();
  },
  chengeTimeType(e) {
    let v = e.currentTarget.dataset.time;
    let selectData = this.data.selectData
    selectData.time_type=v;
    this.setData({
      selectData: selectData
    });
    this.setData2MF();
  },
  setRules() {
    let rules = this.data.rules;
    rules.afternoon = this.data.afternoon;
    rules.morning = this.data.morning;
    this.setData({
      rules: rules
    });
    var pages = getCurrentPages(); // 获取页面栈
    var prevPage = pages[pages.length - 2]; // 上一个页面
    prevPage.setData({
      test: this.data.rules
    })
   
    // wx.redirectTo({
    //   url: '/pages/doctorPages/my/addModal/addModal?rules=' + JSON.stringify(this.data.rules),
    // });
    wx.navigateBack({
      delta:1      
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let rules = options.rules;
    if (rules) {
      rules = JSON.parse(rules)
      console.log(rules)
      this.setData({
        rules: rules,
        morning: rules.morning,
        afternoon: rules.afternoon,
        selectData: rules.morning
      })
    }
    console.log(this.data.morning)
    this.setData({
      selectData: this.parseData(this.data.morning)
    })
  },
  //对数组
  parseData(data) {
    data.number = parseInt(data.number)
    return data;
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})